package com.linkoog.devtools.code_generator.wizard.generate.database.table.panel.portion;

import com.linkoog.devtools.code_generator.database.domain.TableInfo;
import com.linkoog.devtools.code_generator.util.StringUtil;
import com.linkoog.devtools.code_generator.wizard.generate.database.table.custom.ColumnInfoJTable;
import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.core.GridLayoutManager;
import com.intellij.util.ui.JBUI;

import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;

public class DbtableRightPanelBuilder {


    private JPanel myMainPanel;
    private JTextField tableNameJTextField;
    private JTextField entityNameJTextField;
    private JTextField commentTextField;

    private JScrollPane columnInfoScrollPane;
    private JTable columnInfoTable;

    private TableInfo tableInfo;

    public DbtableRightPanelBuilder() {
        $$$setupUI$$$();
        this.myMainPanel.setMinimumSize(JBUI.size(195, -1));
        this.myMainPanel.setPreferredSize(JBUI.size(195, -1));
        this.myMainPanel.setBorder(new LineBorder(Color.LIGHT_GRAY));

        this.tableNameJTextField.setEditable(false);
        this.columnInfoScrollPane.setBorder(BorderFactory.createEmptyBorder());

        this.entityNameJTextField.addFocusListener(new FocusAdapter() {
            @Override
            public void focusLost(FocusEvent e) {
                String text = entityNameJTextField.getText();
                if (tableInfo != null && StringUtil.isNotBlank(text)) {
                    tableInfo.setJavaEntityName(text);
                }
            }
        });

        this.commentTextField.addFocusListener(new FocusAdapter() {
            @Override
            public void focusLost(FocusEvent e) {
                String text = commentTextField.getText();
                if (tableInfo != null && StringUtil.isNotBlank(text)) {
                    tableInfo.setTableComment(text);
                }
            }
        });
    }

    public void resetContextInfo(TableInfo tableInfo) {
        this.tableInfo = tableInfo;
        if (tableInfo == null) {
            tableNameJTextField.setText("");
            entityNameJTextField.setText("");
            commentTextField.setText("");

            getColumnInfoTable().getTableInfoModel().clear();
        } else {
            tableNameJTextField.setText(tableInfo.getTableName());
            entityNameJTextField.setText(tableInfo.getJavaEntityName());
            commentTextField.setText(tableInfo.getTableComment());

            getColumnInfoTable().getTableInfoModel().reset(tableInfo.getColumnInfos());
        }
    }


    public JPanel getMyMainPanel() {
        return myMainPanel;
    }

    public ColumnInfoJTable getColumnInfoTable() {
        return (ColumnInfoJTable) columnInfoTable;
    }

    private void createUIComponents() {
        columnInfoTable = new ColumnInfoJTable();
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        createUIComponents();
        myMainPanel = new JPanel();
        myMainPanel.setLayout(new GridLayoutManager(5, 4, new Insets(0, 5, 0, 5), -1, -1));
        final JLabel label1 = new JLabel();
        label1.setText("表名");
        myMainPanel.add(label1, new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_EAST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        tableNameJTextField = new JTextField();
        myMainPanel.add(tableNameJTextField, new GridConstraints(1, 1, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(150, -1), null, 0, false));
        final JPanel panel1 = new JPanel();
        panel1.setLayout(new GridLayoutManager(1, 1, new Insets(0, 0, 0, 0), -1, -1));
        myMainPanel.add(panel1, new GridConstraints(3, 0, 1, 4, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, new Dimension(-1, 6), null, null, 0, false));
        columnInfoScrollPane = new JScrollPane();
        myMainPanel.add(columnInfoScrollPane, new GridConstraints(4, 0, 1, 4, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_WANT_GROW, null, null, null, 0, false));
        columnInfoScrollPane.setViewportView(columnInfoTable);
        final JPanel panel2 = new JPanel();
        panel2.setLayout(new GridLayoutManager(1, 1, new Insets(0, 0, 0, 0), -1, -1));
        myMainPanel.add(panel2, new GridConstraints(0, 0, 1, 4, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, new Dimension(-1, 3), null, null, 0, false));
        final JLabel label2 = new JLabel();
        label2.setText("注释");
        myMainPanel.add(label2, new GridConstraints(2, 2, 1, 1, GridConstraints.ANCHOR_EAST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        final JLabel label3 = new JLabel();
        label3.setText("类名");
        myMainPanel.add(label3, new GridConstraints(2, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        entityNameJTextField = new JTextField();
        myMainPanel.add(entityNameJTextField, new GridConstraints(2, 1, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(150, -1), null, 0, false));
        commentTextField = new JTextField();
        myMainPanel.add(commentTextField, new GridConstraints(2, 3, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(150, -1), null, 0, false));
    }

    /**
     * @noinspection ALL
     */
    public JComponent $$$getRootComponent$$$() {
        return myMainPanel;
    }


}
